package com.song.druid.util;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

public class PinYinUtil {
    public static String getPinYin(String china){
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        format.setVCharType(HanyuPinyinVCharType.WITH_V);
        char[] chars = china.trim().toCharArray();
        String result = "";
        try {
            for (int i = 0; i < chars.length; i++) {
                char c = chars[i];
                if (Character.toString(c).matches("[\\u4e00-\\u9fa5]")){
                    String[] temp = PinyinHelper.toHanyuPinyinStringArray(c, format);
                    result += temp[0];
                }else {
                    result += c;
                }
            }
        } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
            badHanyuPinyinOutputFormatCombination.printStackTrace();
        }
        return result.toUpperCase();
    }

    public static String getFirstShell(String china){
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        format.setVCharType(HanyuPinyinVCharType.WITH_V);
        char[] chars = china.trim().toCharArray();
        String result = "";
        try {
            for (int i = 0; i < chars.length; i++) {
                char c = chars[i];
                if (Character.toString(c).matches("[\\u4e00-\\u9fa5]")){
                    String[] temp = PinyinHelper.toHanyuPinyinStringArray(c, format);
                    result += temp[0].substring(0, 1);
                }else {
                    result += c;
                }
            }
        } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
            badHanyuPinyinOutputFormatCombination.printStackTrace();
        }
        return result.toLowerCase();
    }
}
